/**
 * request-promise模块封装，此模块依赖于request模块
 * @author Huisir q273250950
 * @git https://gitee.com/huisir001/
 * @blog http://www.zuifengyun.com/
 * @example
 * //   http('https://www.apiopen.top/weatherApi', { city: '昔阳' }).then(res => {
 * //       console.log(res.data.city)
 * //   }).catch(err => {
 * //       console.error(err)
 * //   })
 */

const rp = require('request-promise')

/* 封装request-promise */
let http = (uri, data={}, method = 'GET') => {
    let options = { method, uri, json: true },
        reg = /post/i,
        dataKey = reg.test(method) ? 'body' : 'qs'
    options[dataKey] = data
    return rp(options)
}

/* 封装 form-urlencoded 请求 */
http.postForm = (uri, data={})=>{
    let options = { 
        method:'POST', 
        uri, 
        form:data,
        headers: {
            /* 'content-type': 'application/x-www-form-urlencoded' */ // 默认
        }
    }
    return rp(options)
}

/* 封装 formData 请求 */
http.postFormData = (uri, data={})=> {
    let options = { 
        method:'POST', 
        uri, 
        formData:data,
        headers: {
                /* 'content-type': 'multipart/form-data' */ // 默认
        }
    }
    return rp(options)
}

module.exports = http